package com.pingan.stat.reposys.general.service;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.pingan.common.core.property.Properties;
import com.pingan.common.util.ShellExecutor;
import com.pingan.stat.service.StatService;

@Component
public class GeneralStatService implements StatService {

	@Properties(name = "reposys.general.output.path")
	private String generalOutputPath;

	@Properties(name = "scp.shell")
	private String scpShell;

	@Resource
	private ShellExecutor shellExecutor;

	@Resource
	private GeneralUrlStatService generalUrlStatService;

	@Resource
	private GeneralPVUVStatService generalPVUVStatService;

	@Resource
	private GeneralPVUVHourlyStatService generalPVUVHourlyStatService;

	@Resource
	private GeneralVisitPagesStatService generalVisitPagesStatService;

	@Resource
	private GeneralVisitDepthStatService generalVisitDepthStatService;

	@Resource
	private GeneralVisitLengthStatService generalVisitLengthStatService;

	@Resource
	private GeneralVisitFrequenceStatService generalVisitFrequenceStatService;

	@Resource
	private GeneralOldCookieUserStatService generalOldCookieUserStatService;

	@Resource
	private GeneralNewCookieUserStatService generalNewCookieUserStatService;

	@Resource
	private GeneralNetDeviceStatService generalNetDeviceStatService;

	@Resource
	private GeneralNetScreenStatService generalNetScreenStatService;

	@Resource
	private GeneralNetProviderStatService generalNetProviderStatService;

	@Resource
	private GeneralNetBrowserStatService generalNetBrowserStatService;

	@Resource
	private GeneralAfterUrlStatService generalAfterUrlStatService;

	@Resource
	private GeneralReferUrlStatService generalReferUrlStatService;

	@Resource
	private GeneralIndexPageStatService generalIndexPageStatService;

	@Resource
	private GeneralButtonClickStatService generalButtonClickStatService;

	@Resource
	private GeneralButtonClickHourStatService generalButtonClickHourStatService;

	private static final Log log = LogFactory.getLog("hive");

	@Override
	public void dailyStat(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String yyyyMMdd = sdf.format(date);
		String output = generalOutputPath + "/" + yyyyMMdd;

		log.info("----- clean up general output path");
		cleanUpPath(output);

		log.info("------ " + yyyyMMdd);
		generalPVUVStatService.dailyStat(date);
		generalPVUVHourlyStatService.dailyStat(date);
		generalUrlStatService.dailyStat(date);
		generalVisitPagesStatService.dailyStat(date);
		generalVisitDepthStatService.dailyStat(date);
		generalVisitLengthStatService.dailyStat(date);
		generalVisitFrequenceStatService.dailyStat(date);
		generalNewCookieUserStatService.dailyStat(date);
		generalOldCookieUserStatService.dailyStat(date);
		generalNetDeviceStatService.dailyStat(date);
		generalNetScreenStatService.dailyStat(date);
		generalNetProviderStatService.dailyStat(date);
		generalNetBrowserStatService.dailyStat(date);
		generalAfterUrlStatService.dailyStat(date);
		generalReferUrlStatService.dailyStat(date);
		generalIndexPageStatService.dailyStat(date);
		generalButtonClickStatService.dailyStat(date);
		generalButtonClickHourStatService.dailyStat(date);

		log.info("----------------->>>>>>>>>>>>>>>>>> scp general output ");
		String[] scpParams = new String[] { output };
		shellExecutor.excecute(scpShell, scpParams);
	}

	public void cleanUpPath(String output) {
		File file = new File(output);
		if (file.isDirectory()) {
			try {
				FileUtils.forceDelete(file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		file.mkdirs();
	}

}
